// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import gameConfig from "../../engines/configs/gameConfig";
import O1HD, { managers, services, modules } from "../../engines/gameEngine";
import { GAME_MODE } from "../../engines/managers/gameContent/gameMode_json";
import { REWARD_TYPE } from "../../engines/services/userContent/userInterface";
import { MODE_NAME_MAP } from "../Const";
import { PAGE } from "../Enum";
import mainRoot from "../MainRoot";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    private m_mode : number = GAME_MODE.CALLDRAGON;

    @property({type:cc.Label,displayName:"广告进度"})
    private AdProgress : cc.Label = null;
    @property({type:cc.Node,displayName:"广告图标"})
    private AdIcon : cc.Node = null;

    protected onLoad(): void {
        this.RefreshInfo();
    }

    OnEnterBtn(){
        managers().soundMgr.playSound("mainSound/anNiu");
        const mode = GAME_MODE.CALLDRAGON;
        if(services().userSrv.getPower()-gameConfig.MODE_POWER[mode]<0){
            modules().uiControlModule.showUserPropGet(REWARD_TYPE.POWER,gameConfig.ADD_ONCE_POWER);
            return;
        }
        // console.log("包名");

        let completeFun = ()=>{
                O1HD.getManagers().gameMgr.selectMode(GAME_MODE.CALLDRAGON);
            };

        if(!services().userSrv.getModeUnlocked(this.m_mode)){
            services().miniGameSrv.getAppApiInstance().loadAndShowVideoAd(()=>{
                services().userSrv.addModeAdCount(this.m_mode);
            },(e)=>{
                console.error(e);
            },()=>{
                if(services().userSrv.getModeAdCount(this.m_mode)>=gameConfig.MODE_VIDEO_COUNT[this.m_mode]){
                    services().userSrv.UnlockMode(this.m_mode);
                    completeFun();
                }
                this.RefreshInfo();
            },`看广告解锁${MODE_NAME_MAP[this.m_mode]}`);
            return;
        }
        completeFun();
    }
    
    RefreshInfo(){
        // console.log("刷新模式按钮",this.m_mode,services().userSrv.getModeAdCount(this.m_mode));
        this.AdProgress.node.active = !services().userSrv.getModeUnlocked(this.m_mode);
        this.AdProgress.string = `${services().userSrv.getModeAdCount(this.m_mode)}:${gameConfig.MODE_VIDEO_COUNT[this.m_mode]}`;
        this.AdIcon.active = !services().userSrv.getModeUnlocked(this.m_mode);
        // if(this.levelProgress){
        //     this.levelProgress.string = `${mainRoot.LevelSystem.GetFarLevel(this.m_mode)}:${mainRoot.LevelSystem.GetMaxLevel(this.m_mode)}`;
        // }
    }

}
